package sg.edu.nus.iss.cats.util;

/** coverts java.util.Date to String using BeanUtils ***/

import java.text.SimpleDateFormat;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
import java.util.*;



public class StringToDateBeanUtilsConverter implements Converter {
    private String formatPattern = null;

    public void setFormatPattern(String formatPattern) {
        this.formatPattern = formatPattern;
    }

    public Object convert(Class type, Object value) {
        Object result = null;
         if (value != null
            && (value instanceof String)
            && (type == Date.class)) {
            try {
                String s = value.toString();
                SimpleDateFormat formatter =
                    new SimpleDateFormat(formatPattern);
                result = (Object) formatter.parse(s);

            } catch (Exception e) {
                throw new ConversionException ("Invalid conversion to date: " + e);
            }
        }
        return result;
    }
}

